<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--卡片视图区域-->
    <el-card>
      <!--搜索与添加区域-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="queryInfo.name"
                    clearable
                    placeholder="请输入内容"
                    @clear="getUserList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addUser">添加用户</el-button>
        </el-col>
      </el-row>

      <!--用户列表区域  -->
      <el-table v-loading="loading"
                :data="userList"
                border
                stripe
      >
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="电话" prop="phone"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="角色" prop="roleName"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <!--  {{scope.row}}-->
            <el-switch v-model="scope.row.enabled" @change="userEnabledChanged(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!--修改-->
            <el-button icon="el-icon-edit" size="mini" type="primary"
                       @click="openEditUser(scope.row)"
            ></el-button>
            <!--删除-->
            <el-button icon="el-icon-delete" size="mini" type="danger"
                       @click="deleteUser(scope.row.id)"
            ></el-button>
            <!--分配角色-->
            <el-tooltip :enterable="false" content="分配角色" effect="dark" placement="top">
              <el-button icon="el-icon-setting" size="mini" type="warning"
                         @click="openAssRole(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>

      </el-table>
      <!--分页区域-->
      <el-pagination
        :current-page="queryInfo.pageNum"
        :page-size="queryInfo.size"
        :page-sizes="[2, 5, 7, 10]"
        :total=total
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
      <!--添加对话框-->
      <el-dialog
        :visible.sync="dialogVisible"
        title="添加用户"
        width="30%"
        @close="addDialogClosed"
      >
        <!--内容主体区域-->
        <el-form
          ref="addFormRef"
          :model="addForm"
          :rules="addFormRules"
          label-width="70px"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addForm.password" type="password"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="addForm.name"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="phone">
            <el-input v-model="addForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="addForm.email"></el-input>
          </el-form-item>
        </el-form>
        <!--内容底部区域-->
        <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addUserYes">确 定</el-button>
                </span>

      </el-dialog>
      <!--修改对话框-->
      <el-dialog
        :visible.sync="editDialogVisible"
        title="修改用户"
        width="50%"
      >
        <el-form ref="editUserRef" :model="editUser" :rules="addFormRules" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="editUser.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="editUser.phone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="editUser.email"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
               <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editUserYes">确 定</el-button>
                </span>

      </el-dialog>
      <!-- 分配角色对话框-->
      <el-dialog
        :visible.sync="assRoleDialogVisible"
        title="分配角色"
        width="50%"
      >
        <div>
          <p>当前用姓名:{{ userInfo.name }}</p>
          <p>当前用角色:{{ userInfo.roleName }}</p>
          <p>选择新角色:
            <el-select v-model="selectedRoleId" placeholder="请选择">
              <el-option
                v-for="item in allRole"
                :key="item.id"
                :label="item.nameZh"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
        </div>
        <span slot="footer" class="dialog-footer">
                <el-button @click="assRoleDialogVisible= false">取 消</el-button>
                <el-button type="primary" @click="assRoleYes">确 定</el-button>
                </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { Message, MessageBox } from 'element-ui'
import { updateEnabled, userList } from '@/api/emsboot/api'
import { addUr, addUser, allRole, deleteUser, updateUser } from '../../api/emsboot/api'

export default {
  data() {
    return {
      queryInfo: {
        name: '',
        pageNum: 1,
        //当前每页显示多少条数据
        pageSize: 7
      },
      dialogVisible: false,
      userList: [],
      total: 1,
      loading: false,
      //添加用户的表单数据
      addForm: {
        username: '',
        password: '',
        name: '',
        phone: '',
        email: '',
        enabled: true,
        userfase: '',
        role_name: '',
        remark: ''

      },
      //添加表单的验证规则对象
      addFormRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入您的联系方式', trigger: 'blur' },
          { min: 11, max: 11, message: '格式错误', trigger: 'blur' }],
        email: [{ required: true, message: '请输入您的邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }]
      },
      //修改用户对话框
      editDialogVisible: false,
      editUser: {
        id: 1,
        username: '',
        phone: '',
        emil: ''
      },
      //分配角色
      assRoleDialogVisible: false,
      allRole: [],
      selectedRoleId: '',
      userInfo: {}
      //分配角色loading
      // assRoleLoading:false
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    //获取用户列表
    getUserList() {
      this.loading = true
      userList(this.queryInfo).then((res) => {
        if (res) {
          this.userList = res.rows
          this.total = res.total
        }
        this.loading = false
      })
    },
    //分页每页条数
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize
      this.getUserList()
    },
    //当前也数
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage
      this.getUserList()
    },
    //改变状态
    async userEnabledChanged(userInfo) {
      updateEnabled(userInfo).then((res) => {
        if (!res) {
          userInfo.enabled = !userInfo.enabled
        }
      })
      // const res = await this.$http.put('/employee/updateEnabled/' + userInfo.id + '/' + userInfo.enabled);

    },

    addUser() {
      this.dialogVisible = true
    },
    //监听添加用户对话框关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },
    addUserYes() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        addUser(this.addForm).then((res) => {
          // const res = await this.$http.post('/employee/addUser/', this.addForm);
          // console.log(res);
          if (!res) return
          this.dialogVisible = false
          this.getUserList()
        })

      })
    },
    openEditUser(editUser) {
      this.editDialogVisible = true
      this.editUser = editUser
    },
    editUserYes() {
      this.$refs.editUserRef.validate(async valid => {
        if (!valid) return
        updateUser(this.editUser).then((res)=>{
          if (!res) return
          this.editDialogVisible = false
          this.getUserList()
        })
        // const res = await this.putRequest('/employee/updateUser', this.editUser)

      })
    },
    deleteUser(id) {
      MessageBox.confirm('此操作将永久删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        deleteUser('/employee/deleteUser/' + id).then((res) => {
          if (res) {
            this.getUserList()
          }
        })
        // const res = await this.$http.delete('/employee/deleteUser/' + id);

      }).catch(() => {
        Message({
          type: 'info',
          message: '已取消删除'
        })

      })
    },
    async openAssRole(userInfo) {
      this.assRoleDialogVisible = true
      allRole().then((res) => {
        this.allRole = res.obj
        this.userInfo = userInfo
      })
      // const res = await this.$http.get('/employee/allRole');

    },
    async assRoleYes() {
      if (!this.selectedRoleId) {
        return Message.error('请选择要分配的角色')
      }
      addUr('/employee/addUr/' + this.userInfo.id + '/' + this.selectedRoleId).then((res) =>{
        if (!res) return
        this.assRoleDialogVisible = false
        this.getUserList()
      })
      // const res = await this.$http.put()

    }
  }
}
</script>

<style lang="less" scoped>

</style>
